package org.upc.edu.AIAExamples.communication;

import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.tools.testagent.TestAgent;

import java.util.ArrayList;

public class CentralElectrica extends Agent {
	
	ArrayList<ACLMessage> peticions;
	
	public class mrcb extends CyclicBehaviour
    {

		public mrcb(Agent agent) {
			super(agent);
		}

		public void action() {
			ACLMessage msg = receive();
			if(msg==null) return;
			if(msg.getSender().getLocalName().equals("clock")) {
				/*System.out.println();
				System.out.println("tick rebut, enviant electricitat");
				for(ACLMessage m : peticions) {
					myAgent.send(m);
				}
				peticions.clear();*/
			}
			else {
				ACLMessage peticio = new ACLMessage();
				peticio.addReceiver(msg.getSender());
				peticio.setSender(myAgent.getAID());
				peticio.setContent(msg.getContent());
				myAgent.send(peticio);
				//peticions.add(peticio);
				System.out.println("CENTRAL: Peticio rebuda, id: "+msg.getSender().getLocalName()+" valor: "+msg.getContent());
			}
		}
      
    }
	
	protected void setup() {
		peticions = new ArrayList<ACLMessage>();
		mrcb b = new mrcb(this);
		this.addBehaviour(b);
	}
}
